﻿//********************************************************************************//
//创建日期：<创建日期，2016-03-13>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：处理公共相关
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
using Entity;
using YXYLCommon;
using BLL;
using System.Web.SessionState;

namespace Web.ashx
{

    /// <summary>
    /// Common 的摘要说明
    /// </summary>
    /// [Serializable] 
    [Serializable]
    public class Common : IHttpHandler,IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["op"] == "Session")
            {
                JsonRs rs = new JsonRs();
                if (context.Session["_UserName"] == null)
                {
                    rs.Msg = "会话已失效";
                    rs.Data1 = "session";                    
                }
                else
                {
                    rs.State = true;
                }
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "WType")
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(JsonConvert.SerializeObject(BusinessTool.WType));
            } 
            else if(context.Request["op"]=="LoginInfo")
            {
                context.Response.ContentType = "text/plain";
                JsonRs rs = new JsonRs(true, null, this.loginer);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }            
            else if (context.Request["op"] == "ZDList")
            {
                string all=context.Request["all"];
                string condition=!string.IsNullOrEmpty(all)?"S_ZHANDIANBH<>00":(this.loginer.ZhanDianBH == "00" ? "S_ZHANDIANBH='51'" : "S_ZHANDIANBH='" + this.loginer.ZhanDianBH + "'");
                List<DDLItem> list = new DALCommon().GetDDLItemList("YS_JX_ZHANDIAN", "S_ZHANDIANMC", "S_ZHANDIANBH", context.Request["split"], condition, null);
                JsonRs rs = new JsonRs(true, null,list);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "GLKList")
            {
                string all = context.Request["all"];
                string condition = !string.IsNullOrEmpty(all)?null:(this.loginer.ZhanDianBH == "00" ? "S_ZHANDIAN='51'" : "S_ZHANDIAN='" + this.loginer.ZhanDianBH + "'");
                List<DDLItem> list = new DALCommon().GetDDLItemList("YS_JX_GUANLIK", "S_GUANLIKMC", "S_GUANLIKH", "S_ZHANDIAN", context.Request["split"], condition, null);
                JsonRs rs = new JsonRs(true, null, list);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "QSBMList")//地下水主管部门
            {
                List<DDLItem> list = new DALCommon().GetDDLItemList("QS_JX_BUMENXX", "S_BUMENMC", "S_BUMENBH", context.Request["split"],"I_JLZT=0", null);
                list.Insert(0,new DDLItem("　","0"));
                JsonRs rs = new JsonRs(true, null, list);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "YSBMList")//自来水部门
            {
                List<DDLItem> list = new DALCommon().GetDDLItemList("YS_JX_BUMENXX", "S_MINGCHENG", "S_BIANHAO", context.Request["split"], "I_JLZT=0", "S_BIANHAO ASC");
                list.Insert(0, new DDLItem(" ", "0"));
                JsonRs rs = new JsonRs(true, null, list);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "QSYM")//获取取水年月
            {
                JsonRs rs = new JsonRs(true, null, BusinessTool.GetSLYMorZWNY(1,BusinessTool.WType));
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "QSZWYM")//获取取水账务年月
            {
                JsonRs rs = new JsonRs(true, null, BusinessTool.GetSLYMorZWNY(2, BusinessTool.WType));
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "YSZWYM")//获取取水账务年月
            {
                JsonRs rs = new JsonRs(true, null, BusinessTool.GetZWYM());
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private Loginer loginer
        {
            set
            {
                HttpContext.Current.Session["Loginer"] = value;
            }
            get
            {
                Loginer obj =HttpContext.Current.Session["Loginer"] as Loginer;
                if (obj == null)
                {
                    obj = new Loginer();
                    obj.Account = LoginInfo.CurrentUser.Account;
                    obj.UserId = LoginInfo.CurrentUser.UserID;
                    obj.UserName = LoginInfo.CurrentUser.UserName;
                    obj.ZhanDianBH = BusinessTool.ZHANDIANBH();
                    HttpContext.Current.Session["Loginer"] = obj;
                }
                return obj;
            }
        }

    }

    /// <summary>
    /// 返回当前登录人信息
    /// 包含账号 ID,名称,站点,管理块等
    /// </summary>
    [Serializable]
    public class Loginer
    {
        public int UserId { get; set; }
        public string Account { get; set; }
        public string UserName { get; set; }
        public string ZhanDianBH { get; set; }
        public string ZhanDianMC { get; set; }
    }
}